package src;

import src.entity.Fruit;
import src.entity.FruitBuyRecord;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {

    static Map<Integer, Fruit> fruitMap = new HashMap<>();

    //数据库中的商品信息
    static {
        fruitMap.put(1, new Fruit("苹果", new BigDecimal("8")));
        fruitMap.put(2, new Fruit("草莓", new BigDecimal("13")));
        fruitMap.put(3, new Fruit("芒果", new BigDecimal("20")));
    }

    /**
     * 无优惠的水果总价计算
     *
     * @param recordList 水果购买集合
     * @return
     */
    public static BigDecimal ordinaryBuyFruit(List<FruitBuyRecord> recordList) {
        BigDecimal totalPrice = BigDecimal.ZERO;
        for (FruitBuyRecord record : recordList) {
            Fruit fruit = fruitMap.get(record.getId());
            totalPrice = totalPrice.add(fruit.getPrice().multiply(new BigDecimal(record.getCount())));
        }
        return totalPrice;
    }

    /**
     * 草莓8折总价计算
     *
     * @param recordList
     * @return
     */
    public static BigDecimal strawberryDiscount(List<FruitBuyRecord> recordList) {
        BigDecimal totalPrice = BigDecimal.ZERO;
        DecimalFormat df = new DecimalFormat("#0.00");
        for (FruitBuyRecord record : recordList) {
            Fruit fruit = fruitMap.get(record.getId());
            if (fruit.getName().equals("草莓")) {
                totalPrice = totalPrice.add(fruit.getPrice().multiply(new BigDecimal(0.8)).multiply(new BigDecimal(record.getCount())));
            } else {
                totalPrice = totalPrice.add(fruit.getPrice().multiply(new BigDecimal(record.getCount())));
            }
        }
        return new BigDecimal(df.format(totalPrice));
    }

    /**
     * 满100减10块总价计算
     *
     * @param recordList 水果购买集合
     * @return
     */
    public static BigDecimal moneyOffBuyFruit(List<FruitBuyRecord> recordList) {
        BigDecimal totalPrice = BigDecimal.ZERO;
        for (FruitBuyRecord record : recordList) {
            Fruit fruit = fruitMap.get(record.getId());
            totalPrice = totalPrice.add(fruit.getPrice().multiply(new BigDecimal(record.getCount())));
        }
        if (totalPrice.compareTo(new BigDecimal(100)) > 0){
            return totalPrice.subtract(new BigDecimal(10));
        }
        return totalPrice;
    }

    //测试
    public static void main(String[] args) {
        //1
        List<FruitBuyRecord> recordList1 = new ArrayList<>();
        recordList1.add(new FruitBuyRecord(1, 5));
        recordList1.add(new FruitBuyRecord(2, 10));
        System.out.println("顾客A：" + ordinaryBuyFruit(recordList1));
        //2
        List<FruitBuyRecord> recordList2 = new ArrayList<>();
        recordList2.add(new FruitBuyRecord(1, 5));
        recordList2.add(new FruitBuyRecord(2, 10));
        recordList2.add(new FruitBuyRecord(3, 5));
        System.out.println("顾客B：" + ordinaryBuyFruit(recordList2));
        //3
        List<FruitBuyRecord> recordList3 = new ArrayList<>();
        recordList3.add(new FruitBuyRecord(1, 5));
        recordList3.add(new FruitBuyRecord(2, 10));
        recordList3.add(new FruitBuyRecord(3, 5));
        System.out.println("顾客C：" + strawberryDiscount(recordList2));
        //4
        List<FruitBuyRecord> recordList4 = new ArrayList<>();
        recordList4.add(new FruitBuyRecord(1, 5));
        recordList4.add(new FruitBuyRecord(2, 10));
        recordList4.add(new FruitBuyRecord(3, 5));
        System.out.println("顾客D：" + moneyOffBuyFruit(recordList4));
    }
}
